﻿using Ewell.BizComponent.UI.Charge.ViewModel;
using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Ewell.BizComponent.UI.Charge.Views
{
    /// <summary>
    /// OnlinePayView.xaml 的交互逻辑
    /// </summary>
    public partial class OnlinePayView : UserControl
    {
        public static readonly DependencyProperty OnlinePayVmProperty = DependencyProperty
            .Register("onlinePayVm", typeof(OnlinePayVm), typeof(OnlinePayView),
            new FrameworkPropertyMetadata(new OnlinePayVm()));
        private OnlinePayVm _onlinePayVm;
        public OnlinePayVm onlinePayVm {
            get
            {
                if (this.DataContext?.GetType() != typeof(OnlinePayVm))
                    return (OnlinePayVm)GetValue(OnlinePayVmProperty);
                else
                    return this.DataContext as OnlinePayVm;
            }
            set {
                _onlinePayVm = value;
                SetValue(OnlinePayVmProperty, value); 
            }
        }

        public OnlinePayView()
        {
            InitializeComponent();
        }
        public TaskCompletionSource<bool> continueTcs = null;
        public bool payWaitState { get; set; }

        /// <summary>
        /// 支付
        /// </summary>
        internal void PayAll()
        {
            this.Loaded += async (s, e) =>
            {
                await Task.Run(async () =>
                {
                    foreach (var item in this._onlinePayVm.payWayList)
                    {
                        this._onlinePayVm.payWay = item;
                        if (this._onlinePayVm.payWay.hisScanPay && string.IsNullOrEmpty(this._onlinePayVm.payWay.payQrCode))
                        {
                            bool isContinue = await WaitForContinueAsync();
                            if (!isContinue) break;
                        }
                        this._onlinePayVm.Pay(item);
                    }

                });
                this.Visibility = Visibility.Collapsed;
                onlinePayVm.localWindows?.Close();
            };
            onlinePayVm.localWindows?.ShowDialog();
            this.Visibility = Visibility.Visible;
        }
        private async Task<bool> WaitForContinueAsync()
        {
            Dispatcher.Invoke(() =>
            {
                payQrCodeTxt.IsEnabled = true;
                ctnBtn.IsEnabled = true;
                cancelBtn.IsEnabled = true;
            });
            continueTcs = new TaskCompletionSource<bool>();
            return await continueTcs.Task;
        }

        /// <summary>
        /// 退款
        /// </summary>
        internal void RefundAll()
        {
            this.Loaded += async (s, e) =>
            {
                methodLab.Content = "退款";
                await Task.Run(() =>
                {
                    foreach (var item in this.onlinePayVm.payWayList)
                    {
                        this.onlinePayVm.payWay = item;
                        this.onlinePayVm.Refund(item);
                    }
                });
                this.Visibility = Visibility.Collapsed;
                onlinePayVm.localWindows?.Close();
            };
            this.Visibility = Visibility.Visible;
        }
        private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                this.onlinePayVm.payWay.payQrCode = (sender as TextBox).Text.Trim();
                this.PayContinue(sender, e);
            }
        }

        /// <summary>
        /// 继续支付
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PayContinue(object sender, RoutedEventArgs e)
        {
            if (this.onlinePayVm.payWay.hisScanPay && !string.IsNullOrEmpty(this.onlinePayVm.payWay.payQrCode))
            {
                payQrCodeTxt.IsEnabled = false;
                ctnBtn.IsEnabled = false;
                cancelBtn.IsEnabled = false;
                continueTcs.SetResult(true);
            }
        }
        /// <summary>
        /// 终止
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PayCancel(object sender, RoutedEventArgs e)
        {
            if (this.onlinePayVm.payWay.hisScanPay)
            {
                payQrCodeTxt.IsEnabled = false;
                ctnBtn.IsEnabled = false;
                cancelBtn.IsEnabled = false;
                continueTcs.SetResult(false);
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.DataContext = onlinePayVm;
        }
    }
}
